// import { baseConfig } from './request'
import { chatGptResponse } from '@/types/interfaces'
;('use strict')

const URL = 'http://127.0.0.1:3002'

async function* getChatGptMsg(params: chatGptResponse) {
  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      token: JSON.parse(localStorage.getItem('user') || '{}').token,
      'Access-Control-Allow-Origin': '*'
    },
    body: JSON.stringify(params)
  }
  const url = URL + '/gpt/chat'
  const response = await fetch(url, options)
  const reader = response.body?.getReader()

  while (true) {
    const { value, done } = (await reader?.read()) as any
    if (done) {
      yield 'done'
      break // 读取完毕
    } else {
      const chunk = new TextDecoder().decode(value)
      // console.log('原始chunk:', chunk)
      yield chunk
    }
  }
}

export { getChatGptMsg }
